import re
import pyperclip


class TextExtractor:
    """从文本中提取“〇〇は”句式的标题，并保留原句"""

    def __init__(self):
        # 正则匹配“〇〇は”
        self.pattern = re.compile(r'(.+?)は')

    def process_text(self, input_text: str) -> str:
        """处理输入文本"""
        paragraphs = input_text.split('\n')
        result = []

        for para in paragraphs:
            match = self.pattern.search(para)
            if match:
                title = match.group(1)
                result.append(title)
                result.append(para.strip())

        final_result = '\n'.join(result)
        pyperclip.copy(final_result)
        return final_result

    @staticmethod
    def read_input(end_marker="end") -> str:
        """从控制台读取多行文本，直到输入结束标记"""
        print(f"请输入文本（输入 '{end_marker}' 单独一行表示结束）：")
        input_lines = []
        while True:
            line = input()
            if line.strip() == end_marker:
                break
            input_lines.append(line)
        return '\n'.join(input_lines)


if __name__ == "__main__":
    extractor = TextExtractor()
    input_text = extractor.read_input()

    print("\n原始输入：")
    print(input_text)
    print("原始输入行数：", len(input_text.splitlines()))

    output_text = extractor.process_text(input_text)
    print("\n处理完成，结果已复制到剪贴板：")
    print(output_text)
